package com.vim.modules.mall.controller;

import com.github.pagehelper.PageInfo;
import com.vim.common.base.BaseController;
import com.vim.common.utils.JsonUtils;
import com.vim.modules.mall.model.base.Page;
import com.vim.modules.mall.model.mallGoods.MallGoods;
import com.vim.modules.mall.model.mallGoods.MallGoodsResource;
import com.vim.modules.mall.model.mallGoods.MallGoodsType;
import com.vim.modules.mall.service.MallGoodsService;
import com.vim.modules.sys.model.ZTreeNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @作者 Administrator
 * @时间 2020-09-06 13:36
 * @版本 1.0
 */
@Controller
@RequestMapping(value = "/mall/goods")
public class MallGoodsController extends BaseController{

    @Autowired
    private MallGoodsService mallGoodsService;

    /**
     * 商品列表页面
     */
    @RequiresPermissions(value = "mall:goods:list")
    @RequestMapping(value = "/list")
    public String list(){
        return "mall/goods/goodsList";
    }

    /**
     * 商品列表数据
     * @param goodsName 商品名称
     * @param pageNo    页码
     * @param pageSize  每页数量
     */
    @RequiresPermissions(value = "mall:goods:list")
    @RequestMapping(value = "/dataList")
    @ResponseBody
    public PageInfo<MallGoods> dataList(
            @RequestParam(value = "goodsName",required = false) String goodsName,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return mallGoodsService.goodsList(new Page(pageNo,pageSize), goodsName);
    }

    /**
     * 商品分类树列表数据
     */
    @RequestMapping(value = "/ztreeSelectData")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> ztreeSelectData(){
        List<ZTreeNode> nodeList = new ArrayList<>();
        for(MallGoodsType goodsType:mallGoodsService.goodsTypeList()){
            nodeList.add(new ZTreeNode(goodsType.getId(),goodsType.getPid(),goodsType.getName(), false));
        }
        return new ResponseEntity<>(success(nodeList), HttpStatus.OK);
    }

    /**
     * 商品编辑页面
     * @param id 商品id
     */
    @RequiresPermissions(value = "mall:goods:form")
    @RequestMapping(value = "/form")
    public String form(@RequestParam(value = "id", required = false) Long id, Model model){
        if(null != id){
            MallGoods mallGoods = mallGoodsService.goodsInfo(id);
            mallGoods.setPrice(mallGoods.getPrice()/100);
            model.addAttribute("goodsInfo", mallGoods);
            model.addAttribute("pics", StringUtils.join(mallGoods.getResourceList().stream().map(MallGoodsResource::getUrl).toArray(String[]::new), ","));
            model.addAttribute("detail", mallGoods.getDetail());
        }else{
            model.addAttribute("goodsInfo", new MallGoods());
            model.addAttribute("pics", "");
            model.addAttribute("detail", "");
        }
        return "mall/goods/goodsForm";
    }

    /**
     * 商品保存
     * @param mallGoods 基本信息
     * @param pics      商品图片列表，逗号分割
     * @param detail    商品详情，富文本格式
     */
    @RequiresPermissions(value = "mall:goods:form")
    @RequestMapping(value = "/save")
    @ResponseBody
    public ResponseEntity save(MallGoods mallGoods, String pics, String detail){
        List<MallGoodsResource> resourceList = new ArrayList<>();
        Arrays.asList(pics.split(",")).forEach(pic->{
            MallGoodsResource resource = new MallGoodsResource();
            resource.setGoodsId(mallGoods.getId());
            resource.setType("0");
            resource.setUrl(pic);
            resourceList.add(resource);
        });
        mallGoods.setPrice(mallGoods.getPrice()*100);
        mallGoods.setDetail(detail);
        mallGoods.setResourceList(resourceList);
        mallGoodsService.goodsSave(JsonUtils.toString(mallGoods));
        return new ResponseEntity<>(success(), HttpStatus.OK);
    }

}
